package cuongnh.project.japanesediary.vocabulary.ui.view.action.menucontribution.vocabulary.grammars;

import java.util.Set;

import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.window.Window;
import org.eclipse.ui.IViewActionDelegate;
import org.eclipse.ui.IViewPart;

import cuongnh.project.japanesediary.vocabulary.common.CommonServices;
import cuongnh.project.japanesediary.vocabulary.model.Vocabulary;
import cuongnh.project.japanesediary.vocabulary.model.Grammar;
import cuongnh.project.japanesediary.vocabulary.ui.dialog.AddRelatedGrammarDialog;
import cuongnh.project.japanesediary.vocabulary.ui.view.VocabularyGrammarsView;

public class AddVocabularyGrammarAction implements IViewActionDelegate {

	public static final String ID = "cuongnh.project.japanesediary.vocabulary.action.AddVocabularyGrammar";

	protected IViewPart m_view;
	
	protected TableViewer tableViewer;

	private CommonServices commonServices;

	private Vocabulary vocabulary;
	
	private Set<Grammar> selectedGrammars;

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ui.IViewActionDelegate#init(org.eclipse.ui.IViewPart)
	 */
	@Override
	public void init(IViewPart view) {
		m_view = view;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
	 */
	@Override
	public void run(IAction action) {
		// 履歴ビュー更新
		VocabularyGrammarsView view = (VocabularyGrammarsView) m_view
				.getAdapter(VocabularyGrammarsView.class);
		commonServices = view.getCommonServices();
		vocabulary = view.getCurrentVocabulary();
		AddRelatedGrammarDialog relatedGrammarDialog = new AddRelatedGrammarDialog(
				view.getSite().getShell(), commonServices);
		if (relatedGrammarDialog.open() == Window.OK) {
			selectedGrammars = relatedGrammarDialog
					.getSelectedGrammars();
			if (!selectedGrammars.isEmpty()) {
				// update selected vocabulary to current vocabulary
				Vocabulary currentVocabulary = commonServices
						.getVocabularyDao().getVocabularyWithGrammars(
								vocabulary.getVocabularyId());
				currentVocabulary.getGrammars().addAll(selectedGrammars);
				commonServices.getVocabularyDao()
						.update(currentVocabulary);

				for(Grammar selectedGrammar : selectedGrammars){
					// update current vocabulary for selected vocabulary
					selectedGrammar = commonServices.getGrammarDao()
							.getGrammarWithVocabularies(
									selectedGrammar.getGrammarId());
					selectedGrammar.getVocabularies().add(
							currentVocabulary);
					commonServices.getGrammarDao().merge(
							selectedGrammar);
				}				
			}
		}
		view.update();
		tableViewer = view.getTblvGrammars();
		tableViewer.setSelection(new StructuredSelection(selectedGrammars.toArray()));
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action
	 * .IAction, org.eclipse.jface.viewers.ISelection)
	 */
	@Override
	public void selectionChanged(IAction action, ISelection selection) {

	}

}
